{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3fe4636d-a5ee-4aae-8b2b-b78f463419b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n",
      "李四\n",
      "王五\n",
      "张三,李四,王五\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "张三,李四,王五"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Student:\n",
    "    def __init__(self, student_list):\n",
    "        self.student = student_list\n",
    "    \n",
    "    def __getitem__(self, item):\n",
    "        return self.student[item]\n",
    "\n",
    "    def __str__(self):\n",
    "        return \",\".join(self.student)\n",
    "\n",
    "    def __repr__(self):\n",
    "        return \",\".join(self.student)\n",
    "\n",
    "student = Student([\"张三\",\"李四\",\"王五\"])\n",
    "\n",
    "for item in student:\n",
    "    print(item)\n",
    "\n",
    "# 当使用print进行实例化对象打印时， 自动调用__str__方法\n",
    "print(student)\n",
    "\n",
    "# 调试模式 __repr__ 用的较少\n",
    "student\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc53fd96-8e5f-4d2c-8b67-a66107232509",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "当前的魔术方法使python自带的，跟类没有关系，不能自己去实现魔术方法\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9ddff39d-2d02-4000-b91b-0c2a3d3efd83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 绝对值\n",
    "class Nums:\n",
    "    def __init__(self, num):\n",
    "        self.num = num\n",
    "    \n",
    "    # 绝对值\n",
    "    def __abs__(self):\n",
    "        return abs(self.num)\n",
    "\n",
    "my_num = Nums(-3)\n",
    "abs(my_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2711d777-0fd6-46b4-9fa3-d5f1cbc3e12a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x:3, y:5\n"
     ]
    }
   ],
   "source": [
    "# 向量运算\n",
    "class myVector:\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "\n",
    "    def __add__(self, other_instance):\n",
    "        return myVector(self.x + other_instance.x, self.y + other_instance.y)\n",
    "    \n",
    "    def __str__(self):\n",
    "        return f\"x:{self.x}, y:{self.y}\"\n",
    "\n",
    "ve = myVector(1,2)\n",
    "ve2 = myVector(2,3)\n",
    "print(ve + ve2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "077223d6-6324-45d5-af89-ed6cb106df19",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
